﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace GWModflow.FileLayers
{
    public class PBasic : Package
    {
        public PBasic()
        {
            this._fileType = FileType.BAS6;

            this.Fname = "solution.ba6";

            this.Nunit = 11;

            _IBOUND = new List<int[,]>();

            _STRT = new List<double[,]>();

        }

        string _Options = "FREE";

        public string Options
        {
            get { return _Options; }
            set
            {
                if (value.Length <= 199) _Options = value;
                ///else 
            }
        }

        List<int[,]> _IBOUND;

        public List<int[,]> IBOUND
        {
            get { return _IBOUND; }
            set { _IBOUND = value; }
        }




        /// <summary>
        /// 
        /// </summary>
        double _HNOFLO = 1;


        /// <summary>
        /// 稳定单元的水头值，这个值不会发生变化
        /// </summary>
        public double HNOFLO
        {
            get { return _HNOFLO; }
            set { _HNOFLO = value; }
        }


        List<double[,]> _STRT;


        /// <summary>
        /// 运行第一次的初始水头值，一般不会影响计算结果
        /// </summary>
        public List<double[,]> STRT
        {
            get { return _STRT; }
            set { _STRT = value; }
        }




        public override void WriteFile(string path)
        {

            string content = "";

            content += "# HuanAnKeJi BAS File\r\n";

            //添加option(最少有一行)
            //if (_Options == null || _Options.Trim().Length == 0)
            //    content += "NO OPTIONS\r\n";
            //else
            content += _Options.ToUpper() + "\r\n";

            //添加IBOUND
            //if (_Options.ToUpper().Contains("XSECTION"))
            //{
            //    //暂未考虑
            //}
            //else
            //{
            counter = 1;
            foreach (int[,] ibound in _IBOUND)
            {
                if (IsConstant(ibound))
                {
                    content += "CONSTANT  " +string.Format("{0,10}", ibound[0, 0]) + "  IBOUND  " + counter + "\r\n";
                }
                else
                {
                    if (GetArrayString(ibound, ref helpString))
                    {
                        content += "INTERNAL   1 (FREE)  9" + "\r\n";// counter + "\r\n";
                        content += helpString;
                    }
                }
                counter++;
            }
            //}

            //添加HNOFLO 10个字符不足左补空格除非item1使用FREE
            //if (_Options.ToUpper().Contains("FREE"))
            //{
            content += _HNOFLO.ToString("0.00000E+00 ") + "  HNOFLO\r\n";
            //}
            //else
            //{
            //    //暂未考虑
            //    if(_HNOFLO.ToString().Length<=10)  content += string.Format("{0,10}", _HNOFLO) + "    HNOFLO\r\n";
            //    else content += _HNOFLO.ToString(" 0.000e+00") + "    HNOFLO\r\n";
            //}

            //添加STRT 
            //if (_Options.ToUpper().Contains("XSECTION"))
            //{
            //    //暂未考虑
            //}
            //else
            //{
            counter = 1;
            foreach (double[,] strt in _STRT)
            {
                if (IsConstant(strt))
                {
                    content += "CONSTANT  " + strt[0, 0].ToString("0.00000E+00 ") + "  STRT   " + counter + "\r\n";
                }
                else
                {
                    if (GetArrayString(strt, ref helpString))
                    {
                        //////////////////////////////////////-1为暂时 未设置
                        content += "INTERNAL   1 (FREE)  19" + "\r\n"; //counter + "\r\n";
                        content += helpString;
                    }
                }
                counter++;
            }
            //}




            string fname = path + this.Fname;


            FXnaInfrastructure.FileHelper.WriteFile(fname, content);

        }

    }
}
